﻿using Ay.Framework.WPF.Controls;
using MPSystem.Models;
using Base.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;
using Ay.Framework.WPF;

namespace ADX.Views
{
    /// <summary>
    /// ZCYLPage.xaml 的交互逻辑
    /// </summary>
    public partial class ZCYLPage_loop : Page
    {
        Common common = new Common();

        private List<double> P1 = new List<double>() { 10, 34.6, 60, 80 };
        private List<double> P2 = new List<double>() { 11, 54, 70, 800 };
        private List<double> P3 = new List<double>() { 13, 45, 70, 90 };
        private List<string> test = new List<string>() { "正常", "正常1", "正常1", "正常2" };
        private List<double> P12 = new List<double>();
        private List<string> S = new List<string>() { "正常" };
        private List<double> F1 = new List<double>();
        private List<double> F2 = new List<double>();
        private List<double> F3 = new List<double>();
        private List<double> F12 = new List<double>();
        private List<double> Freq = new List<double>();
        private List<DateTime> T = new List<DateTime>();
        private bool _IsBusy = false;
        public bool IsBusy
        {
            get { return _IsBusy; }
            set {  _IsBusy=value ; }
        }
        private double UpperLimit = 40;//上限值
        private double LowLimit = 20;//下限值
        private string ComanpyName = "";
        private bool FirstReflash = true;
        
        private string bh = "", dtStart = "", dtEnd = "", wz = "", hSpan = "", MPa = "",sfzj="0", SYZT="" ;
        string DW = "MPa";
        List<Dictionary<string, string>> conditons = new List<Dictionary<string, string>>();

        public ZCYLPage_loop()
        {
            InitializeComponent();
            Loaded += AyPage_Loaded;
        }

        void AyPage_Loaded(object sender, RoutedEventArgs e)
        {
            Loaded -= AyPage_Loaded;
            var rcq = ConfigService.GetCQ();
            //工作面
            var rgzm = ConfigService.GetGZM(true);
            
            combcq.ItemsSource = rcq;
            combgzm.ItemsSource = rgzm;

            UpperLimit = common.ZCYL_UpperLimt();
            LowLimit = common.ZCYL_LowLimt();
            ComanpyName = common.GetCompanyName();
            T_CompanyName.Text = ComanpyName + "工作面循环阻力";

        }

        private void LayoutRoot_load(object sender, EventArgs e)
        {
            SycnReflash();


        }
        private void btnquery_Click(object sender, RoutedEventArgs e)
        {
            SycnReflash();
        }


        /// <summary>
        /// 后台加载
        /// </summary>
        void SycnReflash()
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (o, ea) =>
            {
                //no direct interaction with the UI is allowed from this method
                AyThread.Instance.RunUI(() =>
                {
             
                    conditons= ConditonsModel.CondionListDic;
                    if (conditons == null) { }
                    else
                    if (conditons.Count > 0)
                    {
                        foreach (var rr in conditons)
                        {
                            Dictionary<string, string> aa = rr;
                            foreach (var drr in aa)
                            {
                                if (drr.Key == "bh") bh = drr.Value;
                                if (drr.Key == "dtStart") dtStart = drr.Value;
                                if (drr.Key == "dtEnd") dtEnd = drr.Value;
                                if (drr.Key == "wz") wz = drr.Value;
                                if (drr.Key == "hSpan") hSpan = drr.Value;
                                if (drr.Key == "MPa") MPa = drr.Value;
                                if (drr.Key == "sfzj") sfzj = drr.Value;
                                if (drr.Key == "SYZT") SYZT = drr.Value;                       
                               

                            }
                        }
                    }
                    DataZcService dzs = new DataZcService();
                    int cq = 0, gzm = 0;//测区，工作面
                    try
                    {
                        cq = int.Parse(combcq.SelectedValue.ToString());
                        gzm = int.Parse(combgzm.SelectedValue.ToString());
                    }
                    catch
                    {
                    } 
                 
                    DateTime db = AyCommon.ToDateTime(dtStart), de = AyCommon.ToDateTime(dtEnd);
                    var r = dzs.CurveNormal(cq,gzm, bh, db, de);
                    //CurveZc rt = new DataZcService().CurveNormal(1, 1, new DateTime(2017, 12, 1), new DateTime(2017, 12, 2));
                    //rt.T为时间，rt.P1为左柱

                    //P1 = r.P1;
                    //P2 = r.P2;
                    P3 = r.P3;
                    P12 = r.P12;
                    S = r.S;
                    T = r.T;
                    F1 = r.F1;
                    F1 = r.F2;
                    F1 = r.F3;
                    F12 = r.F12;
                    Freq = r.Freq;
                    // if()//默认整架 
                    Dictionary<int, List<double>> D = new Dictionary<int, List<double>>();
                   
                    if (sfzj == "0")//整架
                    {
                        if (MPa == "MPa") { D.Clear(); D.Add(0, P12); DW = "MPa"; }//p12
                        else if (MPa == "KN") { D.Clear(); D.Add(0, F12); DW = "KN"; }//F12
                    }
                    if (SYZT == "ZZ")//ZZ左柱 YZ 右柱  BSY 不使用
                    { D.Clear(); D.Add(0, P1); DW = "MPa"; }//P1
                   // if(D.Count>0)
                  //  CreateChartColumn("历史工作阻力",T, D, DW);
                    D.Clear(); D.Add(0, P1); D.Add(1, P2); DW = "MPa";
                    CreateChartColumn("历史工作阻力", test, D, DW);


                });
                //CreateChartColumn("测试", strListx, strListy);
            };
            worker.RunWorkerCompleted += (o, ea) =>
            {
                //work has completed. you can now interact with the UI
                busyin.IsBusy = false;
            };
            //set the IsBusy before you start the thread
            busyin.IsBusy = true;
            worker.RunWorkerAsync();
        }

        public void CreateChartColumn(string name, List<string> valuex, Dictionary<int, List<double>> VY, string dw)
        {
            //创建一个图标
            // Chart chart = new Chart();
            this.chart.Series.Clear();
            if (FirstReflash)
            {
                //设置图标的宽度和高度
                //chart.Width = 580;
                //chart.Height = 380;

                //是否启用打印和保持图片
                chart.ToolBarEnabled = true;

                //设置图标的属性
                chart.ScrollingEnabled = false;//是否启用或禁用滚动
                chart.View3D = false;//3D效果显示

                //创建一个标题的对象
                Title title = new Title();

                //设置标题的名称
                title.Text = Name;

                //向图标添加标题
                chart.Titles.Add(title);




            }

            // 设置数据点              
            DataPoint dataPoint;
            //创建第一个DataSeries
            int next = 0;
            foreach (var item in VY)
            {
                List<double> valuey = item.Value;
                double a = valuey.Max();
          
             
              
                DataSeries oneDataSeries = new DataSeries();
                oneDataSeries.LabelEnabled = false;
                oneDataSeries.LabelText = "#YValue";

                if (next == 1)
                {
                    oneDataSeries.AxisYType = AxisTypes.Secondary;
                    oneDataSeries.LegendText = "活体缩量线";
                    oneDataSeries.Color = new SolidColorBrush(Colors.LightGreen);
                    Axis axisYT = new Axis() { AxisType = AxisTypes.Secondary };

                    axisYT.AxisMinimum = 0;
                    axisYT.IntervalType = IntervalTypes.Number;
                    axisYT.Interval = a / 10;//第二个Y轴刻度要算
                                             //Y轴数据单位 
                    axisYT.Suffix = "mm"; 
                    axisYT.AxisType = AxisTypes.Secondary; ;
                    chart.AxesY.Add(axisYT);
                }
                else
                {
                    oneDataSeries.Color = new SolidColorBrush(Colors.Yellow);
                    oneDataSeries.LegendText = "末阻力曲线";
                    Axis yAxis = new Axis();
                    //设置图标中Y轴的最小值永远为0           
                    yAxis.AxisMinimum = 0;
                    yAxis.Interval = a / 10;//第一个个Y轴刻度要算
                    yAxis.AxisType = AxisTypes.Primary; ;
                    //设置图表中Y轴的后缀          
                    yAxis.Suffix = "MPa";
                    chart.AxesY.Add(yAxis);



                }


                oneDataSeries.RenderAs = RenderAs.Line;

                //创建第一个DataSeries的DataPoint
                DataPoint oneDataPoint = null;
                for (int i = 0; i < valuey.Count; i++)
                {
                    oneDataPoint = new DataPoint();
                    oneDataPoint.AxisXLabel = valuex[i].ToString();
                    var y = (valuey[i]);

                    oneDataPoint.YValue = y;

                    oneDataSeries.DataPoints.Add(oneDataPoint);
                }
                chart.Series.Add(oneDataSeries);
                next++;

            }
            if (FirstReflash)
            {
                TrendLine tl = new TrendLine();
                tl.Value = UpperLimit;
                tl.LineThickness = 2;
                tl.LabelText = "初始力曲线" + UpperLimit + dw;
                tl.LineColor = new SolidColorBrush(Colors.LightPink);
                chart.TrendLines.Add(tl);


                //gr.Children.Add(chart);
                //XX.Children.Add(gr);
            }
        }
        /// <summary>
        /// 高级查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnH_Click(object sender, RoutedEventArgs e)
        {
            ShowNavigationWindow("检测报表--附加信息", "/Views/YFQ/ZCYLPage_History_Condition.xaml", 600, 500);
        }
        /// <summary>
        /// 附加信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAttac_Click(object sender, RoutedEventArgs e)
        {
            ShowNavigationWindow("检测报表--附加信息", "/Views/YFQ/ZCYLPage_Attac.xaml", 600, 500);
        }
        /// <summary>/// 弹出层对象/// </summary>

        public static NavigationWindow window = null;
        /// <summary>/// 使用NavigationWindow弹出页面/// </summary>

        private void ShowNavigationWindow(string title, string uri, int width, int height)
        {
            window = new NavigationWindow();
            window.Title = title;
            window.Width = width;
            window.Height = height;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.ResizeMode = ResizeMode.NoResize;
            window.Source = new Uri(uri, UriKind.Relative);
            window.ShowsNavigationUI = false;
            window.Closing += Window_Closing;
            // window.pas += new window.PassValuesHandler(ReceiveValues);
            window.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            SycnReflash();

            // 整架模式，不加权

            //public CurveZc CurveNormal(int cqbh, int bh, DateTime dtStart, DateTime dtEnd)
            //返回数据里面，T是时间序列，P1（左柱）、P2（右柱）、P3是压力Mpa，P12是整架，F和P类似，单位是Kn


        }


        private void combcq_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (combcq.SelectedValue == null) return;
            int cqbh = AyCommon.ToInt(combcq.SelectedValue);
            var gzmSource = ConfigService.GetGZM(cqbh);
            combgzm.ItemsSource = common.LoadCombox_IECobx(gzmSource);
        }
    }
}
